define(["jquery", "easy-admin"], function ($, ea) {

    var upload = layui.upload,
        table = layui.table,
        layer = layui.layer;

    var init = {
        table_elem: '#currentTable',
        table_render_id: 'currentTableRenderId',
        index_url: 'dealer.order_batch/index',
        add_url: 'dealer.order_batch/add',
        edit_url: 'dealer.order_batch/edit',
        delete_url: 'dealer.order_batch/delete',
        // export_url: 'dealer.order_batch/export',
        modify_url: 'dealer.order_batch/modify',
        refund_url: 'dealer.order_batch/refund',
        order_import_url: '/admin/dealer.order_batch/order_import',
        // order_import_url: '/admin/mall.presale_order/splitOrder',
        export_url: '/admin/dealer.delivery_order/export',
    };

    return {
        index: function () {
            ea.table.render({
                init: init,
                toolbar: ['refresh'],
                cols: [[
                    // {type: "checkbox"},
                    {field: 'order_batch', title: '批次编号' ,width:200},
                    {field: 'sum_order', title: '订单数量' ,width:100},
                    {field: 'goods_images', title: '商品图', minWidth: 80, templet: ea.table.lazyimg ,search:'false'},
                    {field: 'sum_order_amount', title: '总金额', minWidth: 120 ,search:'false'
                        ,templet: function(d){
                            if (d.sum_order_amount == null) {
                                return'￥0.00';
                            } else {
                                return '￥'+d.sum_order_amount;
                            }
                        }},
                    {field: 'sum_order_goods', title: '总实发商品', minWidth: 100 ,search:'false',templet: function(d){
                        return d.goods_name+' *'+(Number(d.sum_order_goods)+Number(d.sum_order_gift));
                    }},
                    {field: 'sum_order_goods', title: '总商品信息', minWidth: 200 ,search:'false',templet: function(d){
                        if (d.sum_order_gift > 0) {
                            return' 商品:'+d.sum_order_goods+' 赠品:'+d.sum_order_gift;
                        } else {
                            return' 商品:'+d.sum_order_goods;
                        }
                    }},
                    {field: 'min_batch_status', title: '状态', minWidth: 100 ,search:'false',templet: function(d){
                        if (d.min_batch_status == 1) {
                            return '待回填';
                        } else if (d.min_batch_status == 2) {
                            return '已回填';
                        } else {
                            return '-';
                        }
                    }},
                    {field: 'max_export_time_title', minWidth: 160, title: '导出时间', search: 'false'},
                    {field: 'max_import_time_title', minWidth: 160, title: '回填时间', search: 'false'},
                    {
                        // fixed: 'right',
                        width: 260,
                        title: '操作',
                        templet: function(e){
                            // 导出
                            var l ='<button class="layui-btn layui-btn-xs custom-action-button" data-id="'+ e.order_batch +'">重新导出</button>'
                            // 导入
                            // l += '<a class="layui-btn layuimini-btn-success layui-btn-xs" data-open="'+init.order_import_url+'" data-title="导入批次订单" data-full="false">导入批次订单</a>'
                            if (e.min_batch_status == 1) {
                                l += '<a class="layui-btn layuimini-btn-success layui-btn-xs" data-open="dealer.order_batch/order_import" data-title="回填单号" data-full="false">回填单号</a>'
                            }
                            return l;
                        }
                    },
                ]],
            });
            // 重新导出
            $(document).on('click', '.custom-action-button', function(e){
                e.preventDefault(); // 阻止默认行为（如果有的话）
                var button = $(this);
                var order_batch = button.data('id'); // 获取按钮批次号
                console.log(order_batch);
                var index = ea.msg.confirm('根据批次号['+order_batch+']进行导出，确定导出？', function () {
                    window.location = init.export_url + (init.export_url.indexOf('?') !== -1 ? '&' : '?') + '&order_batch=' + order_batch;
                    layer.close(index);
                    table.reload(tableId);
                });
            });
            // 上传 Excel 文件
            // upload.render({
            //     elem: '#uploadExcel'
            //     ,url: init.order_import_url // 替换为你的后端接口 URL
            //     ,accept: 'file' // 只允许上传文件
            //     ,exts: 'xls|xlsx' // 只允许上传 Excel 文件
            //     ,before: function(obj){
            //         // 上传前的回调
            //         layer.msg('上传中，请稍候...', {icon: 16, shade: 0.3, time: 2000});
            //     }
            //     ,done: function(res){
            //         // 上传完成后的回调
            //         layer.closeAll('loading'); // 关闭所有加载层
            //         if(res.code === 0){
            //             layer.msg('上传成功！');
            //         } else {
            //             layer.msg('上传失败：' + res.msg);
            //         }
            //     }
            //     ,error: function(){
            //         // 上传出错时的回调
            //         layer.msg('上传出错');
            //     }
            // });

            ea.listen();
        },
        add: function () {
            ea.listen();
        },
        edit: function () {
            ea.listen();
        },
        order_import: function () {
            // console.log(222);
            // 上传 Excel 文件
            upload.render({
                elem: '#uploadExcel'
                ,url: init.order_import_url // 替换为你的后端接口 URL
                ,accept: 'file' // 只允许上传文件
                ,exts: 'xls|xlsx' // 只允许上传 Excel 文件
                ,before: function(obj){
                    // 上传前的回调
                    layer.msg('上传中，请稍候...', {icon: 16, shade: 0.3, time: 2000});
                }
                ,done: function(res){
                    // 上传完成后的回调
                    layer.closeAll('loading'); // 关闭所有加载层
                    if(res.code === 0){
                        // layer.msg('上传成功！');
                        layer.msg(res.msg, {icon: 1, time: 2000}, function () {
                            parent.location.reload();
                        });
                    } else {
                        layer.msg('上传失败：' + res.msg);
                    }
                }
                ,error: function(){
                    // 上传出错时的回调
                    layer.msg('上传出错');
                }
            });
            ea.listen();
        },
    };
});